package com.liuyi.netty.im.lychat.config;

import com.liuyi.netty.im.server.ImServer;
import com.liuyi.netty.im.server.config.LimController;
import com.liuyi.netty.im.server.handle.service.MessageListener;
import com.liuyi.netty.im.server.handle.service.MessageService;
import com.liuyi.netty.im.server.handle.service.RelationChainService;
import com.liuyi.netty.im.server.handle.service.SessionService;
import io.netty.channel.nio.NioEventLoopGroup;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class ImConfig implements DisposableBean {

    private ImServer imServer;

    @PostConstruct
    public void start() {
        imServer = new ImServer(new NioEventLoopGroup(), new NioEventLoopGroup(), 8810, 8811);
        imServer.start();
    }

    @Bean
    public LimController limController(RelationChainService relationChainService, SessionService sessionService, MessageService messageService, MessageListener messageListener) {
        return new LimController(relationChainService, sessionService, messageService, messageListener);
    }

    @Override
    public void destroy() throws Exception {
        if (imServer != null) {
            imServer.shutdown();
        }
    }
}
